/*
 * Groupon-J - Java client library for Groupon API v2
 *  Copyright (C) 2011  Jessica M. Chung
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package jessmchung.groupon.demo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import jessmchung.groupon.GrouponApiV2;
import jessmchung.groupon.GrouponException;
import jessmchung.groupon.datatypes.Deal;
import jessmchung.groupon.datatypes.DealOptions;
import jessmchung.groupon.datatypes.RedemptionLocation;
import jessmchung.groupon.datatypes.Tag;

/**
 *
 * @author Jessica
 */
public class GrouponJDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        GrouponApiV2 client = new GrouponApiV2("249faa689a317d41a4ad643e4f8feea8e86147f2");
        try {
            /*Collection<Division> divisions = client.getDivisions();
            i = divisions.iterator();
            while(i.hasNext()){
                Division division = (Division)i.next();
                System.out.println("*******************************");
                System.out.println(division);
            }*/
            
        	List<Tag> grouponTags = new ArrayList<Tag>();
        	grouponTags.add(new Tag("Dinner Theater"));
        	grouponTags.add(new Tag("Cooking Classes"));
        	grouponTags.add(new Tag("Bagel Shops"));
        	grouponTags.add(new Tag("Bakeries"));
        	grouponTags.add(new Tag("Breweries"));
			grouponTags.add(new Tag("Butchers & Meat Shops"));
			grouponTags.add(new Tag("Candy Stores"));
			grouponTags.add(new Tag("Cheese Shops"));
			grouponTags.add(new Tag("Chocolate Shops"));
			grouponTags.add(new Tag("Espresso Bars"));
			grouponTags.add(new Tag("Gourmet Foods"));
			grouponTags.add(new Tag("Ice Cream & Frozen Yogurt")); 
			grouponTags.add(new Tag("Juice Bars & Smoothies")); 
			grouponTags.add(new Tag("Snack Bars"));
			grouponTags.add(new Tag("Bars"));
			grouponTags.add(new Tag("Champagne Bars")); 
			grouponTags.add(new Tag("Cigar Bars"));
			grouponTags.add(new Tag("Cocktail Bars"));
			grouponTags.add(new Tag("Irish Pubs"));
			grouponTags.add(new Tag("Piano Bars"));
			grouponTags.add(new Tag("Pubs"));
			grouponTags.add(new Tag("Sports Bars")); 
			grouponTags.add(new Tag("Restaurants"));

        	HashMap<String, String> query = new HashMap<String, String>();
        	query.put("lat", "39.118353");
        	query.put("lng", "-76.678054");
        	query.put("radius", "30");
            for(Deal deal : client.getDeals(query)){
            	// skip sold out deals
            	if(deal.getIsSoldOut())
            		continue;
            	
            	// skip deals that end before the next day
                if(deal.getEndAt().before(new Date(System.currentTimeMillis() + 86400000)))
                	continue;
                
                // skip deals without the right tags
                boolean tagFound = false;
                for(Tag tag : deal.getTags()) {
                	if(grouponTags.contains(tag)) {
                		tagFound = true;
                		break;
                	}
                }
                if(!tagFound)
                	continue;
            	
                for(DealOptions dealOptions : deal.getOptions()) {
                	
                	// skip sold out deals
	                if(dealOptions.getIsSoldOut())
	                	continue;
	                
	                // skip deals that end before the next day
	                if(dealOptions.getExpiresAt().before(new Date(System.currentTimeMillis() + 86400000)))
	                	continue;
	                
                	for(RedemptionLocation redemptionLocation : dealOptions.getRedemptionLocations()) {
		                System.out.println("****************");
                		System.out.println(deal.getAnnouncementTitle());
                		System.out.println(dealOptions.getTitle());
		                System.out.println(dealOptions.getExpiresAt());
		                System.out.println(dealOptions.getPrice());
		                System.out.println(dealOptions.getDetails());
		                System.out.println(redemptionLocation.toString());
		                System.out.println(dealOptions.getBuyUrl());
		                System.out.println("****************");
                	}
                }
            }
        } catch (GrouponException ex) {
            System.out.println(ex);
        } catch(Exception ex){
            System.out.println(ex);
        }
    }

}
